package com.seemsea.dao.impl.admin;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.seemsea.dao.AbstractGenericDAO;
import com.seemsea.dao.admin.IdiomDao;
import com.seemsea.entity.admin.idiom.Idiom;

@Repository
public class IdiomDaoImpl  extends AbstractGenericDAO<Idiom, String> implements IdiomDao {
	@Override
	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}
	
	@SuppressWarnings("unchecked")
	public List<Idiom> getIdioms(Idiom idiom){
		StringBuffer paramsBuffer = new StringBuffer();
		if(idiom != null){
			if(idiom.getName() != null && !idiom.getName().equals("")){
				paramsBuffer.append(" and i.name like '%"+idiom.getName()+"%' ");
			}
			
			if(idiom.getSpinyin() != null && !idiom.getSpinyin().equals("")){
				paramsBuffer.append(" and i.spinyin like '%"+idiom.getSpinyin()+"%' ");
			}
		}
		StringBuffer sqlBuffer = new StringBuffer("select distinct i.* from idiom_idiom i where 1 = 1 ");
		sqlBuffer.append(paramsBuffer);
		StringBuffer bufCnt = new StringBuffer();
		bufCnt.append("select count(*) from (").append(sqlBuffer).append(") as ii");
		Query query = entityManager.createNativeQuery(sqlBuffer.toString(), Idiom.class);
		Query queryCnt = entityManager.createNativeQuery(bufCnt.toString());
		if(idiom.getPageSize() !=0){
			query.setFirstResult((idiom.getPage() - 1) * idiom.getPageSize());
			query.setMaxResults(idiom.getPageSize());
		}
		int totalCount = new Integer(queryCnt.getSingleResult().toString());//总记录数
		idiom.setCount(totalCount);
		int pageCount = 0;//页数
		if(idiom.getPageSize() != 0) {
			pageCount = totalCount / idiom.getPageSize();
			if(totalCount % idiom.getPageSize() != 0) {
				pageCount ++;
			}
		}
		idiom.setPageCount(pageCount);
		
		if(pageCount == idiom.getPage()){
			idiom.setCurrentSize(totalCount -  idiom.getPageSize() * (idiom.getPage() - 1));
		} else if(pageCount < idiom.getPage()) {
			idiom.setCurrentSize(0);
		} else {
			idiom.setCurrentSize(idiom.getPageSize());
		}
		return query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public int getIdiomsCount(Idiom idiom){
		StringBuffer paramsBuffer = new StringBuffer();
		if(idiom != null){
			if(idiom.getName() != null && !idiom.getName().equals("")){
				paramsBuffer.append(" and i.name like '%"+idiom.getName()+"%' ");
			}
			
			if(idiom.getSpinyin() != null && !idiom.getSpinyin().equals("")){
				paramsBuffer.append(" and i.spinyin like '%"+idiom.getSpinyin()+"%' ");
			}
		}
		StringBuffer sqlBuffer = new StringBuffer("select distinct i.* from idiom_idiom i where 1 = 1 ");
		sqlBuffer.append(paramsBuffer);
		StringBuffer bufCnt = new StringBuffer();
		bufCnt.append("select count(*) from (").append(sqlBuffer).append(") as ii");
		Query queryCnt = entityManager.createNativeQuery(bufCnt.toString());
		int totalCount = new Integer(queryCnt.getSingleResult().toString());//总记录数
		return totalCount;
	}
}
